/**
 * 
 */
package com.xbang.attendance.schedule;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.xbang.attendance.entity.stock.Inventory;
import com.xbang.attendance.entity.stock.InventoryWarningRecord;
import com.xbang.core.dao.search.component.SearchComponent;
import com.xbang.core.schedule.ScheduleProcess;
import com.xbang.core.service.BaseService;
import com.xbang.core.service.SendMailService;

/**
 * @author Administrator
 * 
 */
public class SendEmailScheduleProcess implements ScheduleProcess {

	private BaseService baseService;

	private SendMailService sendMailService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.xbang.core.schedule.ScheduleProcess#process()
	 */
	public void process() throws Exception {
		if (true){
			return;
		}
		String hqlSentence = "FROM InventoryWarningRecord inventorywarningrecord WHERE 1 = 1";
		SearchComponent searchComponent = new SearchComponent();
		searchComponent.addSearchElement("eq.inventorywarningrecord.snedEmail", false);
		searchComponent.addSearchElement("orderby.inventorywarningrecord.inventory.id", "ASC");

		List<InventoryWarningRecord> inventoryWarningRecords = baseService.searchForList(hqlSentence, searchComponent);

		List<Map<Integer, List<String>>> toMails = new ArrayList<Map<Integer, List<String>>>();
		Map<Integer, Inventory> inventorys = new HashMap<Integer, Inventory>();

		if (inventoryWarningRecords != null) {
			for (InventoryWarningRecord inventoryWarningRecord : inventoryWarningRecords) {
				for (Map<Integer, List<String>> mailMap : toMails) {
					if (mailMap.containsKey(inventoryWarningRecord.getInventory().getId())) {
						mailMap.get(inventoryWarningRecord.getId()).add(
								"仓储产品预警：仓储" + inventoryWarningRecord.getWarehouse().getName() + " 产品 : " + inventoryWarningRecord.getProduct().getProductName()
										+ inventoryWarningRecord.getDescription() + "\n");
					} else {
						List<String> mails = new ArrayList<String>();
						mails.add("仓储产品预警：仓储" + inventoryWarningRecord.getWarehouse().getName() + " 产品 : "
								+ inventoryWarningRecord.getProduct().getProductName() + inventoryWarningRecord.getDescription() + "\n");
						Map<Integer, List<String>> toMap = new HashMap<Integer, List<String>>();
						toMap.put(inventoryWarningRecord.getInventory().getId(), mails);
						toMails.add(toMap);
						inventorys.put(inventoryWarningRecord.getInventory().getId(), inventoryWarningRecord.getInventory());
					}
				}
			}

			for (Map<Integer, List<String>> mailMap : toMails) {
				for (Iterator<Map.Entry<Integer, List<String>>> mailMapIteratorys = mailMap.entrySet().iterator(); mailMapIteratorys.hasNext();) {
					Entry<Integer, List<String>> mailMapIterator = mailMapIteratorys.next();

					Inventory inventory = inventorys.get(mailMapIterator.getKey());
					List<String> msgs = mailMapIterator.getValue();
					String sendMessage = "";
					for (String msg : msgs) {
						sendMessage = sendMessage.concat(msg);
					}

					sendMailService.send(inventory.getWarehouse().getEmail(), inventory.getWarehouse().getName(), "经销商物流管理系统预警邮件", sendMessage);
				}
			}
		}

	}

	/**
	 * @return the baseService
	 */
	public BaseService getBaseService() {
		return baseService;
	}

	/**
	 * @param baseService the baseService to set
	 */
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	/**
	 * @return the sendMailService
	 */
	public SendMailService getSendMailService() {
		return sendMailService;
	}

	/**
	 * @param sendMailService the sendMailService to set
	 */
	public void setSendMailService(SendMailService sendMailService) {
		this.sendMailService = sendMailService;
	}
	
	
}
